import sys
import nltk
from nltk.chunk import RegexpParser
from nltk.tree import Tree

def tagSentence(sentence):
	words = nltk.word_tokenize(str(sentence))
	tags = nltk.pos_tag(words)
	
	return tags

def loadGrammar(infile):
	grammar = ""
	
	f = open(infile, "r")
	
	for pattern in f:
		pattern = pattern.strip()
		
		grammar = grammar + pattern + "\n"
	
	f.close()
	
	return grammar

def chunkPhrase(sentence_tagged):
	grammar = loadGrammar("grammar.txt")
	
	Chunker = RegexpParser(grammar)
	
	parse_tree = Chunker.parse(sentence_tagged)
	
	return parse_tree

def extractPhrase(parse_tree):
	i = 0
	total = len(parse_tree)
	
	phrase_type = ["NP", "VP", "PP", "AP", "ADVP"]
	phrase = []
	
	while(i < total):
		if(isinstance(parse_tree[i], Tree)):
			type = parse_tree[i].node
			
			if(type in phrase_type):
				words = parse_tree[i].leaves()
				
				temp = ""
				for word in words:
					temp = temp + word[0] + " "
				
				phrase.append([type, temp.strip()])
			
			if(parse_tree[i].height > 3):
				phrase = phrase + extract_phrase(parse_tree[i])
		
		i += 1
	
	return phrase

def extractNounPhrase(parse_tree):
	i = 0
	total = len(parse_tree)
	
	phrase = []
	
	while(i < total):
		if(isinstance(parse_tree[i], Tree)):
			type = parse_tree[i].node

			if(type == "NP"):
				words = parse_tree[i].leaves()
				
				temp = ""
				for word in words:
					temp = temp + word[0] + " "
				
				phrase.append(temp.strip())
			
			if(parse_tree[i].height > 3):
				phrase = phrase + extractNounPhrase(parse_tree[i])
		
		i += 1
	
	NPList = []
	
	for NP in phrase:
		if(not(NP in NPList)):
			NPList.append(NP)
	
	return NPList

if __name__ == '__main__':
        a = "As of September 14, Google Desktop will no longer be available for download, and existing installations will not be updated to include new features or fixes."
	#print tagSentence("Barack Obama said that he must improve this organization.")
        b = chunkPhrase(tagSentence("Barack Obama said that he must improve this organization"))
	#print extractNounPhrase(chunkPhrase(tagSentence("Barack Obama said that he must improve this organization.")))
        print extractNounPhrase(chunkPhrase(tagSentence(a)))
